import { Component } from '@angular/core';
import { NavController,Events  } from 'ionic-angular';
import { AlertController } from 'ionic-angular';
import { BarcodeScanner } from '@ionic-native/barcode-scanner';
import { HTTP } from '@ionic-native/http';
import   $  from 'jquery';

import { Alipay, AlipayOrder } from '@ionic-native/alipay';
import { BrowserTab } from '@ionic-native/browser-tab';

import {IndexPage} from "../index/index";//全局
import { ChongPage } from '../chong/chong';//充值
import{AboutPage} from '../about/about';//个人中心

import { ThemeableBrowser, ThemeableBrowserOptions, ThemeableBrowserObject } from '@ionic-native/themeable-browser';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
    alipayOrder: AlipayOrder;
    // 声明变量
    huabi={
				"max":"100",
				"gaokai":"50",
				"gaokaishouyibi": "2.04",
				"dikai":"50",
				"dikaishouyibi":"1.95",
        "shoupan":"3157.87+10.42+0.33%",
        "time":"42:56:18",
        "month":"6",
        "day":"56",
        'days':'',
        "total":"",
        "ke":"0",
        "money":"0",
        'cai':"",
        "cai2":'',
       'daoji':'',
       'daojis':''
    }; 
    jie={
        'jieguo':"涨",     //涨跌
        "bili":"1.94",     //比例
        "qianshu":"0",   //盈亏
        'time':"16:00",
        'daoji':'',
        'daojis':''
    };  

    GKM:string='';                                //高开押注金额
    DKM:string='';                                 //低开押注金额
    isKai_jie:boolean=false;//开盘结果    4
    isDate:boolean=false;   //开盘      3
    isDao:boolean=false;    //开盘结束   0
    isShou:boolean=false;   //收盘      1
    isShous:boolean=false;  //收盘结束   2
    // isShou_jie:boolean=false;//收盘结果   5
    xiazhu:string;
    data = new Date();    
    data_hous = this.data.getHours();
    data_mouth = this.data.getMinutes();
    data_second = this.data.getSeconds();
  public url:string ="http://118.31.79.20:8080/";
  // public url:string ='http://192.168.1.124:8080/';
  // public url:string ="http://192.168.1.124:8080/";
  public gpurl:string=this.url+"Stock/share/shareInfo.do";//股票信息
  public yhurl:string=this.url+"Stock/share/shareUpOrDown.do";//用户猜涨跌
  public jeurl:string=this.url+"Stock/share/shareNumInfo.do";

    userid:string='';
    username:string='';
    userpassword:string='';

    constructor(public navCtrl: NavController, //控制器
                private barcodeScanner1:BarcodeScanner,  
                private browserTab: BrowserTab,public malert: AlertController,
                private alipay: Alipay,private http: HTTP,private themeableBrowser: ThemeableBrowser
    ){
      this.barcodeScanner1=barcodeScanner1;
      this.navCtrl=navCtrl;
      this.browserTab=browserTab;
     
     
      this.getData();
      this.shuju();
      this.DateTab();
      // this.jie_data();
  }
  
  
      // 页面加载
    ionViewDidLoad(){
     
      this.DateTab();
    }
    // 进入页面加载时
    ionViewDidEnter(){
         this.shu();
         this.DateTab();
         this.load();
         this.shuju();
        // this.jie_data();
    }


    //数据请求方法


    shu(){
       this.userid=localStorage.getItem("userid");
        this.username=localStorage.getItem("username");
        this.userpassword=localStorage.getItem("userpassword");
   }
    /*获取当前的时间*/
    getData(){
            var myData=new Date();//当前的事件
            var mm=myData.getMonth() + 1;
            if(mm<10){
              var m ='0'+mm;
            }
            var dd= myData.getDate();
            var d =String( dd < 10 ? '0'+ dd:dd);
            
          this.huabi.month=m;
          this.huabi.day=d;
          // console.log('日期'+d)
    }
  //请求数据股票
    shuju(){
        
        // console.log("1");
        this.http.get(this.gpurl, {}, {})
        .then(data => {
                console.log("请求成功");
              
              let data_data=JSON.parse(data.data);
              let increase = Number(data_data.increase);
              if( increase > 0){
                  data_data.increase='+'+""+ data_data.increase;
                  $(".date_dw").css("color","red");
              }else{
                $(".date_dw").css("color","green");
              }
              if(data_data.increPer > 0){
                  data_data.increPer='+'+" "+data_data.increPer;
              }
                this.huabi.shoupan=""+data_data.nowpri+" "+data_data.increase + "  "+data_data.increPer+"%";
                
        })
        .catch(error => {
        //  this.mWinds('开盘点'+error);
           
        
        });
    }
     
/**
 * 请求猜盘数据
 */
load(){
        //  console.log('首页id'+this.userid);
        //  console.log('首页账户'+this.username);
        //  console.log('首页密码'+this.userpassword);
         let logisurl:string ="http://118.31.79.20:8080/Stock/login/login.do?username="+this.username+'&&password='+ this.userpassword; 
         // 个人信息请求
            this.http.get(logisurl, {}, {})
             .then(data => {
                //获取的数据
                var datass=JSON.parse(data.data);
              
                this.huabi.money = datass.data.money; //用户牛牛币
                //数据改变
                // alert(IndexPage.userid);
            })
            .catch(error => {
                            
                  // this.mWinds("猜盘数据网络异常");
               
            });


      this.http.get(this.jeurl, {}, {})
            .then(data => {
                    
                    console.log("请求成功");
                    let data_data=JSON.parse(data.data);
                    let up = Number(data_data.up);
                    
                    let down = Number(data_data.down)
                    // alert("进去请求");
                    if(data_data.up > 0||data_data.down > 0 ){
                      //  alert('执行了');
                        
                        let zong=up+down;
                        this.huabi.gaokai=(((up/(zong))*100).toFixed(2)).toString();
                        this.huabi.dikai=(((down/(zong))*100).toFixed(2)).toString();
                        this.huabi.total=String(zong);

                          if(data_data.up>0){    
                                this.huabi.gaokaishouyibi=(zong/up).toFixed(2).toString();        
                          }else{
                                this.huabi.gaokaishouyibi="0";
                          }
                          if(data_data.down>0){
                                  this.huabi.dikaishouyibi=(zong/down).toFixed(2).toString();
                          }else{
                                this.huabi.dikaishouyibi="0";
                          }
                        
                            $(".gaokai").css("width",this.huabi.gaokai+'%');
                            $(".dikai").css("width", this.huabi.dikai + "%");

                  }else{
                    
                    this.huabi.total=String( up + down);
                      this.huabi.gaokai="0";
                      this.huabi.dikai="0";
                    this.huabi.gaokaishouyibi="0";
                    this.huabi.dikaishouyibi="0";
                    
                    }
              }).catch(error => {
                  //  this.mWinds("涨跌网络异常");
                  console.log(error);
                
                });
     }

      //时间切换
      DateTab(){
        
          console.log('切换');
            // 访问接口判断值
            this.http.get('http://118.31.79.20:8080/Stock/share/html.do',{},{})   //接受数据   0--开盘结束显示内容    1--收盘买内容   2--收盘结束内容  3--猜开盘内容
              .then(data => {

                console.log('555555');
                  // console.log("请求成功");
                  let datas=JSON.parse(data.data); 
                  // 状态
                  let datass = datas.state;  
                 if(datass == 0){     //开盘结束 9：00
                  console.log('状态'+0);
                   this.load();
                   this.isDate=false;//开盘
                    this.isDao=true; //开盘结束
                   
                 }else if(datass == 4){
                    this.isDao=false; 
                    this.jie.time='16:00';
                    // 显示结果
                    // 先调取数据
                    this.jie_datas();
                    
                    this.isKai_jie=true;
                   
                 }else if(datass == 1){ // 收盘开始 10：00

                   this.isKai_jie=false;  //结果隐藏
                   this.isShou=true;  //收盘开始
                   this.load();
                   let e = datas.date;
                   console.log('收盘时间'+e);
                   setInterval(()=>{this.tenTime(e)},500);
                   this.huabi.cai='猜涨';
                   this.huabi.cai2='猜跌';
                   console.log('状态'+1);
                 }else if(datass == 2){   //收盘结束 13：00
                    console.log('状态'+2);
                    this.load();
                    this.isShou=false;
                    this.isShous=true;    //收盘隐藏
                 
                 }else if(datass ==5){  
                    // 显示收盘结果
                    console.log('收盘结果'+5);
                    this.isShous=false;
                    // 加载下数据
                     this.jie.time='09:00';
                    //  加载请求数据
                     this.jie_data();
                    // 收盘结果显示
                    this.isKai_jie=true;

                 }else if(datass == 3){   //开盘开始  16：00
                  
                  console.log('状态'+3);
                   this.load();
                   this.isKai_jie=false; //收盘结束隐藏
                   this.isDate=true;  //开盘开始显示
                   let e = datas.date;
                   setInterval(()=>{this.nineTime(e)},500);
                   this.huabi.cai='高开';
                   this.huabi.cai2='低开'; 
                   
                                                  
                 }else if(datass == 7){ //法定节假日  不结算   
                      console.log('状态'+7);
                      this.load();
                      let e = datas.date;
                      console.log('时间'+e);
                      setInterval(()=>{this.nineTime(e)},500);
                      this.huabi.cai='高开';
                      this.huabi.cai2='低开'; 
                      this.isDate=true;   //开盘      3
                      this.isDao=false;    //开盘结束   0
                      this.isShou=false;   //收盘      1
                      this.isShous=false;  //收盘结束   2
                      this.isKai_jie=false;
                      
                 }
              }).catch(error => {
                  console.log(666666);

              });
      } 
    
    //收盘结果数据
    jie_data(){
        // 通过id查询总盈亏
        this.http.post('http://118.31.79.20:8080/Stock/shareInfoshangqi/shareshangqi.do',{"userid":this.userid},{})   //接口发送的数据
                 .then(data => {
                    let jie_data=JSON.parse(data.data); 
                    console.log('结果'+jie_data.yingkui);
                    console.log('涨'+jie_data.zhangdie);
                    if(jie_data.yingkui >0){
                      $('.jie_qianshu').css('color','red');
                    }else{
                      $('.jie_qianshu').css('color','green');
                    }
                    if(jie_data.zhangdie == true){
                      $('.jie_shang_left').css({'color':'#fff','background':'red'});
                      this.jie.jieguo='涨';

                    }else{
                      $('.jie_shang_left').css({'color':'#fff','background':'green'});
                       this.jie.jieguo='跌';
                    }
                    this.jie.qianshu=jie_data.yingkui;   //个人盈亏
                    this.jie.bili=jie_data.bili;        //比例
                }).catch(error => {
                  
                
                });
    } 
    // 开盘数据
     jie_datas(){
        // 通过id查询总盈亏
        this.http.post('http://118.31.79.20:8080/Stock/shareInfoshangqi/shareshangqi.do',{"userid":this.userid},{})   //接口发送的数据
                 .then(data => {
                    let jie_data=JSON.parse(data.data); 
                    // console.log('结果'+jie_data.yingkui);
                    // console.log('涨'+jie_data.zhangdie);
                    if(jie_data.yingkui >0){
                      $('.jie_qianshu').css('color','red');
                    }else{
                      $('.jie_qianshu').css('color','green');
                    }
                    if(jie_data.zhangdie == true){
                      $('.jie_shang_left').css({'color':'#fff','background':'red'});
                      this.jie.jieguo='高';
                    }else{
                      $('.jie_shang_left').css({'color':'#fff','background':'green'});
                       this.jie.jieguo='低';
                    }
                    this.jie.qianshu=jie_data.yingkui;   //个人盈亏
                    this.jie.bili=jie_data.bili;        //比例
                }).catch(error => {
                  
                
                });
    }           



    // 方法  点击事件
    
     deng(){
        this.navCtrl.push(IndexPage);
      }
    //点击高开
    gaokai(){
       $(".scrizzs").css("display","block");
       this.shuju();
      //  this.reqGk();
    }

    backdropclick(e,isShow){
        $(".scrizzs").css("display","none");
         $(".scrizz").css("display","none");
       
    }
    //跳转个人中心 
    ming(){
      this.navCtrl.push(AboutPage);
    }
  
    // 计算
    si(){
     
      this.GKM='50';
      
    }
    ban(){
       this.GKM='100';
    }
    man(){
      this.GKM='500';
    }
     sis(){
     
      this.DKM='50';
      
    }
    bans(){
      this.DKM='100';
    }
    mans(){
     this.DKM='500';
    }
    dikai(){
        this.shuju();
        console.log('低开');
       $(".scrizz").css("display","block");
      //  this.reqDk();
    }

    /**
 * 买高开
 */
  reqGk(){
        console.log('高开下注'+this.GKM);
        if(this.userid==''||this.userid==null){
         
            this.mWinds("您好，请先登录账户!!");
          this.navCtrl.push(IndexPage);
        }else{
          console.log('用户id'+this.userid);
          this.http.post(this.yhurl,{"userid":this.userid,//上传的数据是uid  牛牛币个数  买的是上涨还是下跌  0是下跌1是上涨
                            "money":this.GKM,
                            "upordown":1,
                        },{})   //接口发送的数据
              .then(data => {
                  // console.log("请求成功");
                 let datass=JSON.parse(data.data); 
                
                if(datass.success){
                 
                  this.mWinds("购买成功");
                   $(".scrizzs").css("display","none");
                    this.load();
                    this.shuju();
                    this.DateTab();
                }else{
                  alert(datass.msg);
                    // 购买失败跳转充值页面
                    // if(Number(this.huabi.money) == 0){
                    //     this.mWinds("您好，金币不够请充值！！");
                    //     this.navCtrl.push(ChongPage);
                    // }else{
                    //     this.mWinds("购买失败！！");
                    // }
                   
                }                
              }).catch(error => {
                 
              
              });
        }

      }
      /**
       * 买低开
       */
        reqDk(){
           console.log('低开下注'+this.DKM);
           if(this.userid==''||this.userid==null){
             
               this.mWinds("您好，请先登录账户!!");
              this.navCtrl.push(IndexPage);
            }else{
              this.http.post(this.yhurl,{"userid":this.userid,//上传的数据是uid  牛牛币个数  买的是上涨还是下跌  0是下跌1是上涨
                          "money":this.DKM,
                            "upordown":0,
                        },{})   //接口发送的数据
              .then(data => {
                  // console.log("请求成功");
                 let datass=JSON.parse(data.data); 
                
                if(datass.success){
                 
                 this.mWinds("购买成功");
                  $(".scrizz").css("display","none");
                   this.load();
                   this.shuju();
                   this.DateTab();
                }else{
                  
                  // 购买失败跳转充值页面
                  if(Number(this.huabi.money) == 0){
                       this.mWinds("您好，金币不够请充值！！");
                       this.navCtrl.push(ChongPage);
                  }else{
                       this.mWinds("购买失败！！");
                  }
                 
                }                
              }).catch(error => {
               
              
              });
            }
            
       }

      // 9点倒计时
    nineTime(e){
          // console.log('倒计时'+e);
          var startDate=new Date();
          var endDate=new Date(e);
          // console.log("对的"+endDate);
          var countDown=(endDate.getTime()-startDate.getTime())/1000;
          // console.log('数据类型'+typeof((24*60*60)));
          var h=String(parseInt(String(countDown/(60*60))));
          var m=String(parseInt(String(countDown/60%60)));
          var s=String(parseInt(String(countDown%60)));        
          if(Number(h) < 10){
              h = '0'+h;
              
              // console.log(h);
          }
          if(Number(m) < 10){
            m = '0' + m;
          }  
          if(Number(s) < 10){
            s = '0' + s;
          }
            // $('#ten').text(h+'时'+m+'分'+s+'秒');
            this.jie.daoji=h+'时'+m+'分'+s+'秒';
          if(countDown<=0){
            //  $('#ten').text('活动结束'); 
            this.jie.daoji='活动结束';
          }  
              
    }

    //十点倒计时 
    tenTime(e){
          // console.log('倒计时'+e);
          var startDate=new Date();
          var endDate=new Date(e);
          var countDown=(endDate.getTime()-startDate.getTime())/1000;
          // console.log('数据类型'+typeof((24*60*60)));
          var h=String(parseInt(String(countDown/(60*60)%24)));
          var m=String(parseInt(String(countDown/60%60)));
          var s=String(parseInt(String(countDown%60)));        
          if(Number(h) < 10){
              h = '0'+h;
              
              // console.log(h);
          }
          if(Number(m) < 10){
            m = '0' + m;
          }  
          if(Number(s) < 10){
            s = '0' + s;
          }
          //  $('#tens').text(h+'时'+m+'分'+s+'秒');
            this.jie.daojis=h+'时'+m+'分'+s+'秒';
          if(countDown<=0){
            // console.log(countDown);
            //  $('#tens').text('活动结束');
            this.jie.daojis='活动结束';
          }  
    }



      /**
    * 自定义弹框
    */
    mWind(msg){
        let alert= this.malert.create({
      title :"提示",
      message:msg,
      buttons:[{
        text:'取消',
        handler:()=>{         
        }
      },{
        text:'确定',
        handler:()=>{       
        }
      }],
      
    });
  alert.present();

    }
    /**
     * 无按钮
     * @param msg 
     */
    mWinds(msg){
        let alert= this.malert.create({
      title :"提示",
      message:msg,     
    });
  alert.present();

    }

}
